#include "stdafx.h"
#include "listOperation.h"

ListOperation::ListOperation(FileOperation& op){
	fo = op;
}

int ListOperation::makeChartData(list<Combination>& input,int from, int to){
	list<Combination>::iterator iter;
	int count = 0;
	for(iter = input.begin();iter != input.end();iter++){
		if((*iter).count>= from && (*iter).count < to)
			count++;
	}

	return count;
}
void ListOperation::makeCombinationList(list<Combination>& cList,int type){

	Combination *c = NULL;
	for(int i=0;i<10;i++)
			for(int j=i+1;j<10;j++)
				for(int k=j+1;k<10;k++)
					for(int l=k+1;l<10;l++){
						if (type == 4){
							c = new Combination(i,j,k,l,9999,0);
							cList.push_back(*c);
							delete c;							
						} else if (type == 5) {
							for(int m=l+1;m<10;m++){
								c = new Combination(i,j,k,l,m,0);
								cList.push_back(*c);
								delete c;
							}
						}
					}
}

void ListOperation::makeChartDataList(ChartDataClass& cdc){
	char* token = NULL;
	char* next_token = NULL;

	char* fileName = "chartData.ini";

	ChartData *i = NULL;
	int *a  = new int[6];
	int count = -1;

	int temp=0;

	while(fo.getRecord(token,next_token,fileName)){
		if(strcmp(token," ")==0 || strcmp(token,"\n")==0)
				continue;
		if(temp>=4){
			count = (count+1)%6;
			a[count] = atoi(token);

			if(count%6 == 5){
				i = new ChartData(a[0],a[1],a[2],a[3],a[4],a[5]);
				cdc.lst.push_back(*i);
				delete i;
			}
		} else if(temp == 0){
			temp++;
			cdc.inputCount = atoi(token);
		} else if(temp == 1){
			temp++;
			cdc.i1 = atoi(token);
		} else if(temp == 2){
			temp++;
			cdc.i2 = atoi(token);
		} else if(temp == 3){
			temp++;
			cdc.i3 = atoi(token);
		}
	}
}

void ListOperation::makeInputList(list<InputSet> & inList){

	char* token = NULL;
	char* next_token = NULL;

	char* fileName = "input.txt";


	InputSet *i = NULL;
	int *a  = new int[3];
	int count = -1;

	
	while(fo.getRecord(token,next_token,fileName)){
		if(strcmp(token," ")==0 || strcmp(token,"\n")==0)
			continue;
		count = (count+1)%3;
		a[count] = atoi(token);

		if(count%3 == 2){
			i = new InputSet(a[0],a[1],a[2]);
			inList.push_back(*i);
			delete i;
		}
	} 
}


void ListOperation::makeCustomizeInputList(char* fileName,list<Combination>& srcList, list<Combination>& destList){
	
	list<int> lineLst;
	fo.getLineNum(fileName,lineLst);

	lineLst.sort();

	int count = 0;
	for(list<int>::iterator iter=lineLst.begin();iter != lineLst.end();++iter){
		destList.push_back(getCombinationByAddress(srcList,(*iter)));
	}
	
}

void ListOperation::make3020List(list<Combination>& input1,list<Combination>& input2, list<Combination>& output30, list<Combination>& output20){
	list<Combination>::iterator iter;
	int count = 0;
	for(iter = input1.end(),--iter;count <30;count++,--iter){
		output30.push_back(*iter);
	}

	for(iter = input2.end(),--iter,count=0;count<20;count++,--iter){
	
		output20.push_back(*iter);
	}
}

Combination ListOperation::getCombinationByAddress(list<Combination>&lst, int count){
	list<Combination>::iterator iter = lst.begin();
	while(count>1){
		iter++;
		count--;
	}
	return (*iter);	
}